package com.atguigu.fruit.dao.impl;

import com.atguigu.fruit.bean.Fruit;
import com.atguigu.fruit.dao.FruitDao;
import com.atguigu.fruit.dao.base.BaseDao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author BruceLee 单精度类型数据模式
 * @create 2022-02-28 15:10
 */
public class FruitDaoImpl extends BaseDao implements FruitDao {
//    Connection conn;
//    PreparedStatement psmt;
    String sql;

    @Override
    public List<Fruit> getFruitList() {
        List<Fruit> fruitList = new ArrayList<>();
        try {
            conn = getConn();
            //编写sql语句
            sql = "select * from t_fruit";
            //预编译sql语句
            psmt = conn.prepareStatement(sql);
            rs = psmt.executeQuery();

            //解析rs
            while(rs.next()){
                int fid = rs.getInt(1);
                String fname = rs.getString("fname");
                int price = rs.getInt(3);
                int fcount = rs.getInt(4);
                String remark = rs.getString("remark");

                Fruit fruit = new Fruit(fid, fname, price, fcount, remark);
                fruitList.add(fruit);

            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(rs,psmt,conn);
        }


        return fruitList;
    }

    @Override
    public boolean addFruit(Fruit fruit) {

        try {
            conn = getConn();
            sql = "insert into t_fruit value(0,?,?,?,?)";
            psmt = conn.prepareStatement(sql);

            psmt.setString(1,fruit.getFname());
            psmt.setInt(2,fruit.getPrice());
            psmt.setInt(3,fruit.getFcount());
            psmt.setString(4,fruit.getRemark());

            return (psmt.executeUpdate() > 0);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(rs,psmt,conn);
        }

        return false;
    }

    @Override
    public boolean updateFruit(Fruit fruit) {

        try {
            conn = getConn();
            sql = "update t_fruit set fcount = ? where fid = ?";
            psmt = conn.prepareStatement(sql);
            psmt.setInt(1,fruit.getFcount());
            psmt.setInt(2,fruit.getFid());

            return (psmt.executeUpdate() > 0);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally{
            close(rs,psmt,conn);
        }

        return false;
    }

    @Override
    public Fruit getFruitByFname(String fname) {

        try {
            conn = getConn();
            sql = "select * from t_fruit where fname = ?";
            psmt = conn.prepareStatement(sql);

            psmt.setString(1,fname);
            rs = psmt.executeQuery();
            if(rs.next()){
                int fid = rs.getInt(1);
                int price = rs.getInt(3);
                int fcount = rs.getInt(4);
                String remark = rs.getString(5);
                return new Fruit(fid,fname,price,fcount,remark);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally{
            close(rs,psmt,conn);
        }


        return null;
    }


    @Override
    public boolean delFruit(String fname) {

        try {
            conn = getConn();
            sql = "delete from t_fruit where fname = ?";
            psmt = conn.prepareStatement(sql);
            psmt.setString(1,fname);
            int num = psmt.executeUpdate();
            return num > 0;

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(rs,psmt,conn);
        }

        return false;
    }

    @Override
    public boolean delFruit(Integer fid) {
        try {
            conn = getConn();
            sql = "delete from t_fruit where fid = ?";
            psmt = conn.prepareStatement(sql);
            psmt.setInt(1,fid);
            int num = psmt.executeUpdate();
            return num > 0;

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(rs,psmt,conn);
        }

        return false;
    }
}
